
local U = require "packages/hanqing/util"
local U2 = require "packages/utility/utility"

local yinfeng = fk.CreateSkill{
  name = "hanqing__yinfeng",
  tags = { U.VerticalAlliance },
  attached_skill_name = "hanqing__yinfeng&",
}

Fk:loadTranslationTable{
  ["hanqing__yinfeng"] = "引锋",
  [":hanqing__yinfeng"] = "<a href='hanqing__alliance_skill'>合纵技</a>，"..
  "你可以将武器牌当【杀】、【杀】当【借刀杀人】、【借刀杀人】当任意伤害锦囊牌使用。<br>"..
  "捭阖：一张牌以此法转化时，你可以重铸一张♠牌，令如此转化的牌按上述规则再次转化。",

  ["#hanqing__yinfeng"] = "引锋：将武器牌当【杀】、【杀】当【借刀杀人】、【借刀杀人】当任意伤害锦囊牌使用",
  ["#hanqing__yinfeng-invoke"] = "引锋：你可以重铸一张♠牌，将 %dest 转化的%arg改为%arg2",
  ["hanqing__yinfeng_arg1"] = "【借刀杀人】",
  ["hanqing__yinfeng_arg2"] = "任意伤害锦囊牌",
  ["hanqing__yinfeng_arg3"] = "【杀】",
  ["#hanqing__yinfeng-choose"] = "引锋：选择对 %dest 使用【借刀杀人】的副目标",
  ["#hanqing__yinfeng-choice"] = "引锋：选择修改的牌名",
}

yinfeng:addEffect("viewas", {
  anim_type = "offensive",
  pattern = ".",
  prompt = "#hanqing__yinfeng",
  handly_pile = true,
  interaction = function (self, player)
    local all_names = table.filter(Fk:getAllCardNames("t"), function (name)
      return Fk:cloneCard(name).is_damage_card
    end)
    table.insert(all_names, 1, "slash")
    table.insert(all_names, 2, "collateral")
    local names = player:getViewAsCardNames(yinfeng.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 then
      if self.interaction.data == "slash" then
        return Fk:getCardById(to_select).sub_type == Card.SubtypeWeapon
      elseif self.interaction.data == "collateral" then
        return Fk:getCardById(to_select).trueName == "slash"
      else
        return Fk:getCardById(to_select).trueName == "collateral"
      end
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or self.interaction.data == nil then return end
    local card, c = Fk:getCardById(cards[1])
    if card.sub_type == Card.SubtypeWeapon then
      c = Fk:cloneCard("slash")
    elseif card.trueName == "slash" then
      c = Fk:cloneCard("collateral")
    elseif card.trueName == "collateral" then
      c = Fk:cloneCard(self.interaction.data)
    end
    c.skillName = yinfeng.name
    c:addSubcards(cards)
    return c
  end,
  enabled_at_response = function (self, player, response)
    if response then return end
    local all_names = table.filter(Fk:getAllCardNames("t"), function (name)
      return Fk:cloneCard(name).is_damage_card
    end)
    table.insert(all_names, "slash")
    table.insert(all_names, "collateral")
    return #player:getViewAsCardNames(yinfeng.name, all_names) > 0
  end,
  enabled_at_nullification = Util.FalseFunc,
})

yinfeng:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(yinfeng.name) and
      table.contains(data.card.skillNames, yinfeng.name) and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local arg = ""
    if data.card.trueName == "slash" then
      arg = "hanqing__yinfeng_arg1"
    elseif data.card.trueName == "collateral" then
      arg = "hanqing__yinfeng_arg2"
    else
      arg = "hanqing__yinfeng_arg3"
    end
    local card = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = yinfeng.name,
      pattern = ".|.|spade",
      prompt = "#hanqing__yinfeng-invoke::"..target.id..":"..data.card:toLogString()..":"..arg,
      cancelable = true,
    })
    if #card > 0 then
      if data.card.trueName == "slash" then
        local subTos = {}
        for _, p in ipairs(data.tos) do
          local sub_tos = table.filter(room.alive_players, function (q)
            return Fk:cloneCard("collateral").skill:targetFilter(player, q, {p}, {}, card)
          end)
          if #sub_tos > 0 then
            local sub_to = room:askToChoosePlayers(player, {
              min_num = 1,
              max_num = 1,
              targets = sub_tos,
              skill_name = yinfeng.name,
              prompt = "#hanqing__yinfeng-choose::"..p.id,
              cancelable = true,
            })
            if #sub_to > 0 then
              table.insert(subTos, sub_to)
            else
              return
            end
          end
        end
        event:setCostData(self, {cards = card, tos = {target}, subTos = subTos})
        return true
      elseif data.card.trueName == "collateral" then
        local all_names = table.filter(Fk:getAllCardNames("t"), function (name)
          return Fk:cloneCard(name).is_damage_card
        end)
        local name = U2.askForChooseCardNames(room, player, all_names, 1, 1, yinfeng.name, "#hanqing__yinfeng-choice", all_names, true)
        if #name > 0 then
          event:setCostData(self, {cards = card, tos = {target}, choice = name})
          return true
        end
      else
        event:setCostData(self, {cards = card, tos = {target}})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local name
    if data.card.trueName == "slash" then
      name = "collateral"
      data.subTos = event:getCostData(self).subTos
    elseif data.card.trueName == "collateral" then
      name = event:getCostData(self).choice[1]
    else
      name = "slash"
    end
    data:changeCard(name)
    room:recastCard(event:getCostData(self).cards, player, yinfeng.name)
  end,
})

yinfeng:addAcquireEffect(function (self, player, is_start)
  player.room:handleAddLoseSkills(player, "hanqing__baihe&", nil, false)
end)

yinfeng:addLoseEffect(function (self, player, is_death)
  if not table.find(player:getSkillNameList(), function (s)
    return Fk.skills[s]:hasTag(U.VerticalAlliance) or Fk.skills[s]:hasTag(U.HorizontalAlliance)
  end) then
    player.room:handleAddLoseSkills(player, "-hanqing__baihe&", nil, false)
  end
end)

return yinfeng
